//
//  GroupsHeaderView.m
//  好友列表
//
//  Created by 伍银荣 on 2021/6/8.
//  Copyright © 2021 com.example. All rights reserved.
//

#import "GroupsHeaderView.h"
#import "Groups.h"

@interface GroupsHeaderView ()
@property (nonatomic , weak) UIButton *groupBtn;
@property (nonatomic , weak) UILabel *onlineLabel;


@end

@implementation GroupsHeaderView

+(instancetype)groupsHeaderViewWithTableView:(UITableView *)tableView {
    static NSString *ID = @"headerView";
    GroupsHeaderView *headerView = [tableView dequeueReusableHeaderFooterViewWithIdentifier:ID];
    if (!headerView) {
        headerView = [[GroupsHeaderView alloc]initWithReuseIdentifier:ID];
    }
    return headerView;
}

-(void)setGroups:(Groups *)groups {
    _groups = groups;
    //设置数据
    [_groupBtn setImage:[UIImage imageNamed:@"sanjiaoxing_normal"] forState:UIControlStateNormal];
    [_groupBtn setTitle:_groups.name forState:UIControlStateNormal];
    
    _onlineLabel.text = [NSString stringWithFormat:@"%d / %ld",_groups.online,_groups.friends.count];
    //在重新创建的headerView中会出现cell重用的问题,所以在创建haederView的同时重新将headerView的状态设置一下
    if (groups.visible) {
        self.groupBtn.imageView.transform = CGAffineTransformMakeRotation(M_PI_2);
    } else {
        self.groupBtn.imageView.transform = CGAffineTransformMakeRotation(0);
    }
}

-(instancetype)initWithReuseIdentifier:(NSString *)reuseIdentifier {
    if (self = [super initWithReuseIdentifier:reuseIdentifier]) {
//        self.contentView.backgroundColor = [UIColor redColor];
        //按钮
        UIButton *groupBtn = [[UIButton alloc]init];
        //groupBtn.backgroundColor = [UIColor greenColor];
        [groupBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        //设置按钮的内容左对齐
        groupBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
        //设置按钮的图片的内边距
        groupBtn.imageEdgeInsets = UIEdgeInsetsMake(0, 10, 0, 0);
        //设置按钮文字的内边距
        groupBtn.titleEdgeInsets = UIEdgeInsetsMake(0, 15, 0, 0);
        //按钮的图片内容的显示模式
        groupBtn.imageView.contentMode = UIViewContentModeCenter;
        //设置按钮图片超出的部分不需要截取
        groupBtn.imageView.clipsToBounds = NO;
        [self.contentView addSubview:groupBtn];
        [groupBtn addTarget:self action:@selector(groupBtnClicked) forControlEvents:UIControlEventTouchUpInside];
        _groupBtn = groupBtn;
        
        
        //label
        UILabel *onlineLabel = [[UILabel alloc]init];
       // onlineLabel.backgroundColor = [UIColor blueColor];
        onlineLabel.textAlignment = NSTextAlignmentRight;
        onlineLabel.textColor = [UIColor redColor];
        [self.contentView addSubview:onlineLabel];
        _onlineLabel = onlineLabel;
    }
    return self;
}

- (void)groupBtnClicked {
    NSLog(@"------groupBtnClicked");
    //标记当前按钮点击的状态
    self.groups.visible = !self.groups.isVisible;
    //刷新tableView:相当于把之前tableView里面的cell全部删掉重新创建tableView
    if ([self.delegate respondsToSelector:@selector(groupBtnClickedWithGtoupHeaderView:)]) {
        [self.delegate groupBtnClickedWithGtoupHeaderView:self];
    }
}

//当一个新的headerView已经加到某个父控件中的时候会执行这个方法
-(void)didMoveToSuperview {
    [super didMoveToSuperview];
    //    if (self.groups.visible) {
    //        [_groupBtn setImage:[UIImage imageNamed:@"sanjiaoxing_highlited"] forState:UIControlStateNormal];
    //    } else {
    //        [_groupBtn setImage:[UIImage imageNamed:@"sanjiaoxing_normal"] forState:UIControlStateNormal];
    //    }
    
    if (self.groups.visible) {
        self.groupBtn.imageView.transform = CGAffineTransformMakeRotation(M_PI_2);
    } else {
        self.groupBtn.imageView.transform = CGAffineTransformMakeRotation(0);
    }
}


#pragma mark - 当当前控件headerView的frame发生改变时,会执行这个方法
-(void)layoutSubviews {
    //一定要写这句话,不然会导致按钮的点击事件不响应
    [super layoutSubviews];
    _groupBtn.frame = self.bounds;
    CGFloat labelW = 100;
    CGFloat labelH = self.bounds.size.height;
    CGFloat labelX = self.bounds.size.width - 20 - labelW;
    CGFloat labelY = 0;
    _onlineLabel.frame = CGRectMake(labelX, labelY, labelW, labelH);
}


/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
